from db_connect.conn_mysql import ConnMysql
from db_connect.conn_redis import ConnRedis
from db_connect.conn_slave_mysql import ConnMysqlSlave
import aiomysql
import asyncio


class Methods():
    async def select_info(self, sql, args=None):
        async with ConnMysqlSlave().pool.acquire() as conn:
            async with conn.cursor(aiomysql.DictCursor) as cur:
                if args == None:
                    await cur.execute(sql)
                    result_info = await cur.fetchall()

                else:
                    await cur.execute(sql, args)
                    result_info = await cur.fetchall()
                    if result_info:
                        return result_info
                    else:
                        return result_info

    async def insert_info(self, sql, args=None):
        async with ConnMysql().pool.acquire() as p:
            async with p.cursor(aiomysql.DictCursor) as cur:
                if args == None:
                    await cur.execute(sql)
                    await p.commit()
                    return 'no'
                else:
                    await cur.execute(sql, args)
                    await p.commit()
                    return 'ok'

    async def update_info(self, sql, args=None):
        #更改用户信息
        async with ConnMysql().pool.acquire() as p:
            async with p.cursor(aiomysql.DictCursor) as cur:
                if args == None:
                    await cur.execute(sql)
                    await p.commit()
                else:
                    await cur.execute(sql, args)
                    await p.commit()
                    return 'ok'

    async def select_all(self, sql, args=None):
        #选取用户的信息
        async with ConnMysqlSlave().pool.acquire() as p:
            async with p.cursor(aiomysql.DictCursor) as cur:
                if args == None:
                    await cur.execute(sql)
                    result_info = await cur.fetchone()

                else:
                    await cur.execute(sql, args)
                    result_info = await cur.fetchone()
                    return result_info

    async def delect_info(self, sql, args):
        async with ConnMysql().pool.acquire() as p:
            async with p.cursor(aiomysql.DictCursor) as cur:
                if args == None:
                    await cur.execute(sql)
                    await p.commit()
                else:
                    await cur.execute(sql, args)
                    await p.commit()
                    return 'ok'
